package client

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

func HttpReport(w http.ResponseWriter, r *http.Request) {

	data, err := ioutil.ReadAll(r.Body)
	if err != nil {
		fmt.Println("Read failed:", err)
		return
	}
	defer r.Body.Close()

	//fmt.Println("http Post:", data)

	var nameToInfo map[string]*Info
	err = json.Unmarshal(data, &nameToInfo)
	if err != nil {
		fmt.Println("json.Unmarshal err:", err)
		return
	}
	//fmt.Println("http Post:", nameToInfo)

	RefreshJson(timeInterval, nameToInfo)
}

func HttpStart(localAddr string) {
	fmt.Println("Http Server start On :", localAddr)

	http.HandleFunc("/Report", HttpReport)

	err := http.ListenAndServe(localAddr, nil)
	if err != nil {
		fmt.Println("dir http err:", err)
	}
}
